import AppLayout from '@/core/components/layouts/AppLayout'
import addressApi from '@/lib/address/api/addressApi'
import EditAddressComponent from '@/lib/address/components/EditAddress'

export default function EditAddress({ id, defaultValues }) {
  return (
    <AppLayout title='Ubah Alamat'>
      <EditAddressComponent
        id={id}
        defaultValues={defaultValues}
      />
    </AppLayout>
  )
}

export async function getServerSideProps(context) {
  const { id } = context.query
  const address = await addressApi({ id })
  const defaultValues = {
    type: address.type,
    name: address.name,
    email: address.email,
    mobile: address.mobile,
    street: address.street,
    zip: address.zip,
    city: address.city?.id || '',
    oldDistrict: address.district?.id || '',
    district: '',
    oldSubDistrict: address.subDistrict?.id || '',
    subDistrict: ''
  }
  return { props: { id, defaultValues } }
}
